<?php
// +----------------------------------------------------------------------
// | YFCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: rainfer <81818832@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use phpDocumentor\Reflection\DocBlock\Tag\VarTag;
use think\Db;
use app\admin\model\Page as NewsPage;

class Product extends Base
{
    /**
     * 产品列表
     */
	public function index()
	{
        $key=input('key');
        if(!empty($key)){
            $map['name']= array('like',"%".$key."%");
        }else {
            $map=0;
        }
	    $data = array();
        $product =Db::name('product')->where($map)->paginate(config('paginate.list_rows'),false,['query'=>get_query()]);
        foreach ($product as $k =>$v ) {
            $source=Db::name('category')->where(array('id'=>$v['cid']))->find();
            $ff['id'] = $v['id'];
            $ff['name'] = $v['name'];
            $ff['cate_name'] = $source['name'];
            $ff['sort'] = $v['sort'];
            $ff['pro_type'] = $v['pro_type'];
            $ff['addtime'] = $v['addtime'];
            array_push($data,$ff);
        }
        $show = $product->render();
        $this->assign('page',$show);
        $this->assign("product",$data);
		return $this->fetch();
	}

    /**
     * 添加产品
     * @author tiedong
     */
    public function add()
    {
        $cate_model = Db::name("category")->select();
        $this->assign("cate",$cate_model);
        return $this->fetch();
    }
    
    /**
     * 添加操作
     */
    public function runAdd()
    {
            //处理图片
            $img_url='';
            $file = request()->file('file0');
            if($file){
                if(config('storage.storage_open')){
                    $upload = \Qiniu::instance();
                    $info = $upload->upload();
                    $error = $upload->getError();
                    if ($info) {
                        $img_url= config('storage.domain').$info[0]['key'];
                    }else{
                        $this->error($error,url('admin/product/runAdd'));//否则就是上传错误，显示错误原因
                    }
                }else{
                    $validate=config('upload_validate');
                    $info = $file->validate($validate)->rule('uniqid')->move(ROOT_PATH . config('upload_path') . DS . date('Y-m-d'));
                    if($info) {
                        $img_url=config('upload_path'). '/' . date('Y-m-d') . '/' . $info->getFilename();
                        //写入数据库
                        $data['uptime']=time();
                        $data['filesize']=$info->getSize();
                        $data['path']=$img_url;
                        Db::name('plug_files')->insert($data);
                    }else{
                        $this->error($file->getError(),url('admin/product/runAdd'));//否则就是上传错误，显示错误原因
                    }
                }
            }
            $data = array();
            $data['cid'] = input("cate_id");
            $data['name'] = input("news_title");
            $data['sort'] = input("listorder");
            $data['content'] = htmlspecialchars_decode(input("news_content"));
            $data['pro_type'] = input("pro_type");
            $data['photo_x'] = $img_url;
            $rst = Db::name("product")->insert($data);
            if($rst!==false){
                $this->success('产品添加成功',url('admin/product/index'));
            }else{
                $this->error('产品添加失败',url('admin/product/index'));
            }

    }

    public function edit()
    {
        $id = !empty(input("id"))?input("id"):0;
        if ( empty($id)) {
            $this->error('参数有误！');
        }
        $cate_model = Db::name("category")->select();
        $this->assign("cate",$cate_model);
        $product =Db::name('product')->where("id=$id")->find();
        $this->assign("product",$product);
        return $this->fetch();
    }

    public function runEdit()
    {

        //处理图片
        $img_url='';
        $file = request()->file('file0');
        if($file){
            if(config('storage.storage_open')){
                $upload = \Qiniu::instance();
                $info = $upload->upload();
                $error = $upload->getError();
                if ($info) {
                    $img_url= config('storage.domain').$info[0]['key'];
                }else{
                    $this->error($error,url('admin/product/runAdd'));//否则就是上传错误，显示错误原因
                }
            }else{
                $validate=config('upload_validate');
                if (!empty($file)) {
                    $info = $file->validate($validate)->rule('uniqid')->move(ROOT_PATH . config('upload_path') . DS . date('Y-m-d'));
                    if($info) {
                        $img_url=config('upload_path'). '/' . date('Y-m-d') . '/' . $info->getFilename();
                        //写入数据库
                        $data['uptime']=time();
                        $data['filesize']=$info->getSize();
                        $data['path']=$img_url;
                        Db::name('plug_files')->insert($data);
                    }else{
                        $this->error($file->getError(),url('admin/product/runAdd'));//否则就是上传错误，显示错误原因
                    }
                }

            }
        }
        $data = array();
        $data['id']  = input('id');
        $data['cid'] = input("cate_id");
        $data['name'] = input("news_title");
        $data['sort'] = input("listorder");
        $data['content'] = htmlspecialchars_decode(input("news_content"));
        $data['pro_type'] = input("pro_type");
        !empty($img_url) ?  $data['photo_x'] = $img_url:" ";
        $rst = Db::name("product")->update($data);
        if($rst!==false){
            $this->success('产品修改成功',url('admin/product/index'));
        }else{
            $this->error('产品修改失败',url('admin/product/index'));
        }

    }

    /*
     * 广告位删除
	 * @author rainfer <81818832@qq.com>
     */
    public function del()
    {
        $p = input('p');
        $rst=Db::name('product')->where(array('id'=>input('id')))->delete();//删除该广告位所有广告
        if($rst!==false){
            $this->success('删除成功',url('admin/Product/index', array('p' => $p)));
        }else{
            $this->error('删除失败',url('admin/Product/index', array('p' => $p)));
        }
    }

}